package com.xk.meeting.back.controller;

import com.xk.meeting.entity.Admin;
import com.xk.meeting.service.AdminService;
import com.xk.meeting.service.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/back/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.login(username,password);

        if (admin != null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            response.sendRedirect("applyList");
        }else {
            request.setAttribute("msg","用户名或密码错误,请重新输入");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

    }
}
